<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dung
 * Date: 2/15/14
 * Time: 5:02 PM
 * To change this template use File | Settings | File Templates.
 */
?>
<div id="maincp">
	<h1 class="h1">List Manager</h1>
	<ul class="list">
		<li><a href="index.php?p=addnews" title="Add a news">Add a news</a></li>
	</ul>
    <table class="datatable">
        <thead>
        <tr>
            <td>Title</td>
            <td>Summary</td>
            <td>Date Added</td>
            <td>User Posted</td>
        </tr>
        <thead>
        <?php
        if (isset($_GET["page"])) {
            $page  = $_GET["page"];
        }else{
            $page=1;
        };
        $start = ($page-1) * 10;
        $news=NewsLists($start);
        if(mysql_num_rows($news)){
        ?>
            <?php
            while($row_news=mysql_fetch_array($news))
            {
            ob_start();
            ?>
                <tbody>
            <tr>
                <td>{Title}</td>
                <td>
                    <img src="{Image}" width="100" align="left" />
                    {Summary}
                </td>
                <td>{DatePosted}</td>
                <td>{UserPosted}</td>
                <td><a class="text-dung" href="index.php?p=editnews&IDNews={IDNews}">Edit</a>
                    -
                    <a class="text-dung" href="index.php?p=deletenews&IDNews={IDNews}">Delete</a>
                </td>
            </tr>
                </tbody>
            <?php
            $full=explode(" ",$row_news["date_added"]);
            $rawdate=explode("-",$full[0]);
            $newdate = $rawdate[2]."/".$rawdate[1]."/".$rawdate[0];
            $date=$full[1]." ".$newdate;
            $s= ob_get_clean();
            $s=str_replace("{IDNews}",$row_news["new_id"],$s);
            $s=str_replace("{Title}",$row_news["title"],$s);
            $s=str_replace("{Summary}",$row_news["summary"],$s);
            $s=str_replace("{DatePosted}",$date,$s);
            $s=str_replace("{Image}",$row_news["image"],$s);
            $s=str_replace("{UserPosted}",$row_news["user_id"],$s);
            echo $s;
            ?>
        <?php
        }
        }else{
        ?>
            <tbody>
            <tr>
                <td style="text-align: center" class="no-result" colspan="4">
                    <?php echo "No Result!"; ?>
                </td>
            </tr>
            </tbody>
        <?php }
        ?>
    </table>
    <table width="100%">
        <tr>
            <td>
                <?php
                $qr="SELECT COUNT(new_id) from dt_news";
                $result=mysql_query($qr);
                $row_result=mysql_fetch_array($result);
                $records=$row_result[0];
                $total_pages=ceil($records/10);
                if($total_pages>2){
                    echo "Pages ";
                    for ($i=1; $i<=$total_pages; $i++) {
                        echo "<a href='index.php?p=newslistcp&page=".$i."'>".$i."</a> ";
                    }
                }?>
            </td>
        </tr>
    </table>
	<a href="index.php?p=cpanel" title="Back to Dashboard">&laquo; Back to Dashboard</a>
</div>